package work.net.udp;

import java.net.DatagramPacket;
import java.net.DatagramSocket;


/**
 * 使用UDP接受数据
 */
public class ServerDemo {

    public static void main(String[] args) throws Exception {
        // 1. 创建接收端对象
        DatagramSocket socket = new DatagramSocket(8888);

        // 2. 创建数据包对象
        byte[] buffer = new byte[1024 * 64];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

        // 3. 不断地接受数据
        while (true) {
            socket.receive(packet);
            int len = packet.getLength();
            String res = new String(buffer, 0, len);
            System.out.println("收到了来自" + packet.getAddress() + "的消息: " + res);
        }
    }

}
